package cn.gzsxt.music.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gzsxt.music.query.UserCollectQueryObject;
import cn.gzsxt.music.service.UserCollectServcie;
import cn.gzsxt.music.service.UserService;
import cn.gzsxt.music.vo.MsgObj;

@Controller
@RequestMapping("/userCollect")
public class UserCollectController {
	
	@Autowired
	private UserCollectServcie service;
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("addUsList")
	@ResponseBody
	public MsgObj addUsList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		if (user.size() != 0) {
			Long count = service.checkUsByUnameAndSname(params);
			if (count == 0) {
				int row = service.insertUsList(params);
				if (row == 1) {
					msgObj = new MsgObj(1, "添加收藏成功");
				}else {
					msgObj = new MsgObj(0, "添加收藏失败");
				}
			}else {
				msgObj = new MsgObj(0, "8好意思，该收藏已存在");
			}
			
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("updateUsList")
	@ResponseBody
	public MsgObj updateUsList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		String songList_name = (String) params.get("songList_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		List<Map<String, Object>> songList = service.checkSongListByName(songList_name);
		if (user.size() != 0) {
			if (songList.size() != 0) {
				Map<String, Object> userMap = user.get(0);
				Object user_id = userMap.get("user_id");
				Map<String, Object> songListMap = songList.get(0);
				Object songList_id = songListMap.get("songList_id");
				params.put("user_id", user_id);
				params.put("songList_id", songList_id);
				int row = service.updateUsList(params);
				if (row == 1) {
					msgObj = new MsgObj(1, "修改成功");
				}else {
					msgObj = new MsgObj(0, "修改失败");
				}
			}else {
				msgObj = new MsgObj(0, "该歌单不存在");
			}
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("deleteUsListById")
	@ResponseBody
	public MsgObj deleteUsListById(Long usList_id) {
		int row = service.deleteUsListById(usList_id);
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (row == 1) {
			msgObj = new MsgObj(1, "删除成功");
		}
		return msgObj;
	}
	
	@RequestMapping("addUcList")
	@ResponseBody
	public MsgObj addUcList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		if (user.size() != 0) {
			Long count = service.checkUcByUnameAndCid(params);
			if (count == 0) {
				int row = service.insertUcList(params);
				if (row == 1) {
					msgObj = new MsgObj(1, "添加收藏成功");
				}else {
					msgObj = new MsgObj(0, "添加收藏失败");
				}
			}else {
				msgObj = new MsgObj(0, "8好意思，该收藏已存在");
			}
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("updateUcList")
	@ResponseBody
	public MsgObj updateUcList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		if (user.size() != 0) {
			int row = service.updateUcList(params);
			if (row == 1) {
				msgObj = new MsgObj(1, "修改成功");
			}else {
				msgObj = new MsgObj(0, "修改失败");
			}
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("deleteUcListById")
	@ResponseBody
	public MsgObj deleteUcListById(Long ucList_id) {
		int row = service.deleteUcListById(ucList_id);
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (row == 1) {
			msgObj = new MsgObj(1, "删除成功");
		}
		return msgObj;
	}
	
	@RequestMapping("addUsrList")
	@ResponseBody
	public MsgObj addUsrList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		if (user.size() != 0) {
			Long count = service.checkUsrByUnameAndRid(params);
			if (count == 0) {
				Object user_id = user.get(0).get("user_id");
				params.put("user_id", user_id);
				int row = service.insertUsrList(params);
				if (row == 1) {
					msgObj = new MsgObj(1, "添加收藏成功");
				}else {
					msgObj = new MsgObj(0, "添加收藏失败");
				}
			}else {
				msgObj = new MsgObj(0, "8好意思，该收藏已存在");
			}
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("updateUsrList")
	@ResponseBody
	public MsgObj updateUsrList(@RequestParam Map<String, Object> params) {
		MsgObj msgObj = new MsgObj();
		String user_name = (String) params.get("user_name");
		List<Map<String, Object>> user = userService.checkByUName(user_name);
		if (user.size() != 0) {
			Object user_id = user.get(0).get("user_id");
			params.put("user_id", user_id);
			int row = service.updateUsrList(params);
			if (row == 1) {
				msgObj = new MsgObj(1, "修改成功");
			}else {
				msgObj = new MsgObj(0, "修改失败");
			}
		}else {
			msgObj = new MsgObj(0, "该用户名不存在");
		}
		return msgObj;
	}
	
	@RequestMapping("deleteUsrListById")
	@ResponseBody
	public MsgObj deleteUsrListById(Long usrList_id) {
		int row = service.deleteUsrListById(usrList_id);
		MsgObj msgObj = new MsgObj(0, "删除失败");
		if (row == 1) {
			msgObj = new MsgObj(1, "删除成功");
		}
		return msgObj;
	}
	
	@RequestMapping("getAllSingers")
	@ResponseBody
	public List<Map<String, Object>> getAllSingers() {
		List<Map<String, Object>> result = service.getAllSingers();
		return result;
	}
	
	@RequestMapping("getAllCds")
	@ResponseBody
	public List<Map<String, Object>> getAllCds() {
		List<Map<String, Object>> result = service.getAllCds();
		return result;
	}
	
	@RequestMapping("userCollectPage")
	public String userSongPage() {
		return "userCollectPage";
	}
	
	@RequestMapping("usList")
	@ResponseBody
	public Map<String, Object> usList(UserCollectQueryObject qo){
		System.out.println("UsList的qo对象："+qo);
		Map<String, Object> result = service.selectUsListByQo(qo);
		return result;
	}
	
	@RequestMapping("ucList")
	@ResponseBody
	public Map<String, Object> ucList(UserCollectQueryObject qo){
		System.out.println("UcList的qo对象："+qo);
		Map<String, Object> result = service.selectUcListByQo(qo);
		return result;
	}
	
	@RequestMapping("usrList")
	@ResponseBody
	public Map<String, Object> usrList(UserCollectQueryObject qo){
		System.out.println("UsrList的qo对象："+qo);
		Map<String, Object> result = service.selectUsrListByQo(qo);
		return result;
	}
}
